package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.CardDetail;
import com.cmccgz.udacs_landlord_webapp.bean.CardMsg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cmccgz.udacs_landlord_webapp.main.peoplemanager.XiaoCardActivity.CARD_ACTIVE;
import static com.cmccgz.udacs_landlord_webapp.main.peoplemanager.XiaoCardActivity.CARD_NEGATIVE;


/**
 * @author INX
 *         卡片状态修改adapter
 */
public class CardStateChangedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<CardDetail> datas;
    private Context context;
    private OnCardStateChangeListenter listenter;
    private String emptyText;
    private Map<String,String>typeMapping=new HashMap<>();

    public CardStateChangedAdapter(List<CardMsg.CardAllType> card_all_type,List<CardDetail> datas, Context context, OnCardStateChangeListenter listenter,String emptyText) {
        this.datas = datas;
        this.context = context;
        this.listenter = listenter;
        this.emptyText=emptyText;
        updateType(card_all_type);
    }
    public CardStateChangedAdapter(List<CardMsg.CardAllType> card_all_type,List<CardDetail> datas, Context context, OnCardStateChangeListenter listenter){
        this(card_all_type,datas,context,listenter,null);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return new EmptyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_empty,parent,false));
            case 1:
                return new NormalViewHolder(LayoutInflater.from(context).inflate(R.layout.item_card,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case -1:
                TextView tv_empty = ((EmptyViewHolder) holder).tv_empty;
                if (tv_empty.hasOnClickListeners()==false) {
                    tv_empty.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (listenter!=null) {
                                listenter.onClickEmptyView();
                            }
                        }
                    });
                }
                if (!StringUtils.isEmpty(emptyText)) {
                    tv_empty.setText(emptyText);
                }
                break;
            case 1:
                final CardDetail cardDetail = datas.get(position);
                final NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
                String typeName = typeMapping.get(cardDetail.card_type);
                typeName= StringUtils.isEmpty(typeName)?"未知类型":typeName;
                normalViewHolder.tv_name.setText(typeName);
                normalViewHolder.tv_card_time.setText("有效期:"+cardDetail.card_end_time);
                normalViewHolder.tv_card_num.setText("卡号:"+cardDetail.card_id);
                normalViewHolder.sw_state.setVisibility(View.VISIBLE);
                normalViewHolder.sw_state.setChecked(CARD_ACTIVE.equals(cardDetail.card_state));
                normalViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SwitchCompat sw_state = normalViewHolder.sw_state;
                        sw_state.setChecked(!sw_state.isChecked());
                    }
                });
                normalViewHolder.sw_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        cardDetail.card_state=isChecked?CARD_ACTIVE:CARD_NEGATIVE;
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return datas == null || datas.size() == 0 ? 1 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (datas == null || datas.size() == 0) {
            return -1;
        } else {
            return 1;
        }
    }

    private class EmptyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }

        public void setEmptyText(String text) {
            if (tv_empty!=null) {
                tv_empty.setText(text);
            }
        }
    }

    private class NormalViewHolder extends RecyclerView.ViewHolder{

        private final TextView tv_name;
        private final TextView tv_card_num;
        private final TextView tv_card_time;
        private final SwitchCompat sw_state;

        public NormalViewHolder(View itemView) {
            super(itemView);
            tv_name = ((TextView) itemView.findViewById(R.id.tv_name));
            tv_card_num = ((TextView) itemView.findViewById(R.id.tv_card_num));
            tv_card_time = ((TextView) itemView.findViewById(R.id.tv_card_time));
            sw_state = ((SwitchCompat) itemView.findViewById(R.id.sw_state));
        }
    }
    public interface OnCardStateChangeListenter{
        void onClickEmptyView();
    }
   public void updateType(List<CardMsg.CardAllType>types){
       //组装映射关系
       for (int i = 0; types!=null&&i < types.size(); i++) {
           CardMsg.CardAllType cardAllType = types.get(i);
           typeMapping.put(cardAllType.type_id,cardAllType.type_name);
       }
   }
    public void updateTypeAndNotify(List<CardMsg.CardAllType>types){
        updateType(types);
        notifyDataSetChanged();
    }
}
